﻿using QuanLyGiaoVuService.CommandServices;
using QuanLyGiaoVuService.ReportServices;
using QuanLyGiaoVuUI.Extensions.ExtendedAttributes;
using QuanLyGiaoVuUI.Models.MonHoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QuanLyGiaoVuUI.Controllers
{
    [GiangVienAuthorize(ChiGiaoVu=true)]
    [HandleError]
    public class MonHocController : Controller
    {
        private IMonHocReportService _monHocReportService;
        private IMonHocCommandService _monHocCommandService;
        private ILopMonHocReportService _lopMonHocReportService;
        private IGiangVienReportService _giangVienReportService;

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public MonHocController(IMonHocCommandService monHocCommandService, IMonHocReportService monHocReportService, ILopMonHocReportService lopMonHocReportService, IGiangVienReportService giangVienReportService)
        {
            _monHocCommandService = monHocCommandService;
            _monHocReportService = monHocReportService;
            _lopMonHocReportService = lopMonHocReportService;
            _giangVienReportService = giangVienReportService;
        }

        [HttpGet]
        public ActionResult Index(Guid? maLop)
        {
            var dsLopSinhVien = _lopMonHocReportService.GetAllLopSinhVien().OrderBy(l => l.KhoaNam);
            return View(dsLopSinhVien);
        }

        [HttpGet]
        public ActionResult CapNhat(Guid? id)
        {
            CapNhatMonHocViewModel viewModel = new CapNhatMonHocViewModel();
            if (id != null && !id.Equals(Guid.Empty))
                viewModel = viewModel.FromMonHocDTO(_monHocReportService.GetMonHocById((Guid)id));
            viewModel.DanhSachMonHocChinh = _monHocReportService.GetAllMonHoc(null);
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult CapNhat(CapNhatMonHocViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var monHocDTO = viewModel.ToMonHocDTO();
                if (viewModel.MonTuChon == false)
                    monHocDTO.MaMonChinh = null;
                _monHocCommandService.UpdateMonHoc(monHocDTO);
                return RedirectToAction("Index");
            }
            viewModel.DanhSachMonHocChinh = _monHocReportService.GetAllMonHoc(null);
            return View(viewModel);
        }

        [HttpGet]
        public ActionResult LayDanhSachMonHocTheoLopSinhVien(Guid? maLop)
        {
            ChuongTrinhKhungViewModel viewModel = new ChuongTrinhKhungViewModel();

            if (maLop.HasValue)
            {
                viewModel.MaLop = maLop.Value;
                var dsMonHoc = _monHocReportService.GetAllMonHoc(maLop);
                viewModel.DanhSachMonHoc = MonHocViewModel.FromMonHocDTOList(_monHocReportService.SapXepMonHoc(dsMonHoc).OrderBy(mon => mon.HocKy));
                return View(viewModel);
            }
            else
            {
                return View(viewModel);
            }
        }

        [HttpGet]
        public ActionResult LayDanhSachMonHocTheoBoMon(int? maBoMon)
        {
            MonHocBoMonViewModel viewModel = new MonHocBoMonViewModel();
            if (maBoMon.HasValue)
            {
                viewModel.MaBoMon = maBoMon.Value;
                var dsMonHoc = _monHocReportService.GetAllMonHocByBoMon(maBoMon.Value);
                viewModel.DanhSachMonHoc = MonHocViewModel.FromMonHocDTOList(dsMonHoc.OrderBy(mon => mon.HocKy));
                return View(viewModel);
            }
            else
            {
                return View(viewModel);
            }
        }

        [HttpGet]
        public ActionResult DanhSachMonHoc()
        {
            var dsMonHoc = _monHocReportService.GetAllMonHoc(null);
            IEnumerable<MonHocViewModel> danhSachMonHoc = MonHocViewModel.FromMonHocDTOList(_monHocReportService.SapXepMonHoc(dsMonHoc));
            return View(danhSachMonHoc);
        }

        [HttpGet]
        public ActionResult ThemMonChuongTrinhKhung(Guid? maLop)
        {
            CapNhatChuongTrinhKhungViewModel viewModel = new CapNhatChuongTrinhKhungViewModel();
            var dsMonHoc = _monHocReportService.GetAllMonHocKhongThuocChuongTrinhKhung(maLop);
            viewModel.DanhSachMonHoc = _monHocReportService.SapXepMonHoc(dsMonHoc);
            viewModel.DanhSachLop = _lopMonHocReportService.GetAllLopSinhVien().OrderBy(lop => lop.KhoaNam);
            if (maLop.HasValue)
                viewModel.MaLop = maLop.Value;
            return View(viewModel);
        }

        [HttpPost]
        [GiangVienAuthorize(ChiGiaoVu = true, ReturnJson = true)]
        public JsonResult ThemMonChuongTrinhKhung(CapNhatChuongTrinhKhungViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (_monHocCommandService.InsertMonHocChuongTrinhKhung(viewModel.MaLop, viewModel.MaMonHoc, viewModel.HocKy))
                    return Json(new
                    {
                        success = true
                    });
                else
                    return Json(new { 
                        success = false,
                        message = "Thêm môn học không thành công."
                    });
            }
            else
                return Json(new { 
                    success = false,
                    message = "Chưa nhập đủ thông tin."
                });
        }

        [HttpPost]
        [GiangVienAuthorize(ChiGiaoVu = true, ReturnJson = true)]
        public JsonResult XoaMonChuongTrinhKhung(Guid? maLop, Guid? maMonHoc)
        {
            try
            {
                if (maLop.HasValue && maMonHoc.HasValue)
                {
                    if (_monHocCommandService.DeleteMonHocChuongTrinhKhung(maLop.Value, maMonHoc.Value))
                        return Json(new
                        {
                            success = true
                        });
                    else
                    {
                        return Json(new
                        {
                            success = false,
                            message = "Cập nhật không thành công."
                        });
                    }

                }
                else
                    return Json(new
                    {
                        success = false,
                        message = "Không tìm thấy thông tin."
                    });
            }
            catch (Exception exception)
            {
                return Json(new
                {
                    success = false,
                    message = exception.Message
                });
            }
        }

        [HttpGet]
        public ActionResult BoMon(int? maBoMon)
        {
            BoMonViewModel viewModel = new BoMonViewModel();
            viewModel.DanhSachBoMon = _giangVienReportService.GetAllBoMon();
            viewModel.MaBoMon = maBoMon;
            return View(viewModel);
        }

        [HttpGet]
        public ActionResult ThemMonHocBoMon(int? maBoMon)
        {
            ThemMonHocBoMonViewModel viewModel = new ThemMonHocBoMonViewModel();
            if (maBoMon.HasValue)
            {
                viewModel.MaBoMon = maBoMon.Value;
                viewModel.DanhSachMonHoc = _monHocReportService.GetAllMonHocKhongThuocBoMon(maBoMon.Value);
            }
            else
            {
                viewModel.DanhSachMonHoc = _monHocReportService.GetAllMonHoc(null);
            }

            viewModel.DanhSachBoMon = _giangVienReportService.GetAllBoMon();
            return View(viewModel);
        }

        [HttpPost]
        [GiangVienAuthorize(ChiGiaoVu=true,ReturnJson=true)]
        public JsonResult ThemMonHocBoMon(ThemMonHocBoMonViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _monHocCommandService.InsertMonHocBoMon(viewModel.MaMonHoc, viewModel.MaBoMon);
                    return Json(new
                    {
                        success = true
                    });
                }
                else
                    return Json(new { 
                        success = false,
                        message = "Dữ liệu không hợp lệ."
                    });
            }
            catch (Exception exception)
            {
                log.Error(exception.Message, exception);
                return Json(new { 
                    success = false,
                    message = "Cập nhật không thành công."
                });
            }
        }

        [HttpPost]
        public JsonResult XoaMonHocBoMon(int? maBoMon, Guid? maMonHoc)
        {
            try
            {
                if (maBoMon.HasValue && maMonHoc.HasValue)
                {
                    _monHocCommandService.DeleteMonHocBoMon(maMonHoc.Value, maBoMon.Value);
                    return Json(new
                    {
                        success = true
                    });
                }
                else
                    return Json(new
                    {
                        success = false,
                        message = "Dữ liệu không hợp lệ."
                    });
            }
            catch (Exception exception)
            {
                log.Error(exception.Message, exception);
                return Json(new
                {
                    success = false,
                    message = "Cập nhật không thành công."
                });
            }

        }
    }
}
